Public Class AutoComplete
    Inherits jObjectiveControl

    Private m_ctlInputText As New WebControls.TextBox
    Private m_ctlInputValue As New WebControls.TextBox
    Private m_ctlOptionList As New WebControls.ListBox

    Private m_ctlSID As New Variable
    Private m_ctlWebMethod As New Variable
    Private m_ctlParameter As New Variable
    Private m_ctlOnAutoCompleteSet As New Variable

    Public Property Text() As String
        Get
            Return m_ctlInputText.Text
        End Get
        Set(ByVal value As String)
            m_ctlInputText.Text = value
        End Set
    End Property
    Public Property Value() As String
        Get
            Return m_ctlInputValue.Text
        End Get
        Set(ByVal value As String)
            m_ctlInputValue.Text = value
        End Set
    End Property

    Public Property SID() As String
        Get
            Return m_ctlSID.Value
        End Get
        Set(ByVal value As String)
            m_ctlSID.Value = value
        End Set
    End Property

    Public Property Parameter() As String
        Get
            Return m_ctlParameter.Value
        End Get
        Set(ByVal value As String)
            m_ctlParameter.Value = value
        End Set
    End Property

    Public Overrides Property CssClass() As String
        Get
            Return m_ctlInputText.CssClass
        End Get
        Set(ByVal value As String)
            m_ctlInputText.CssClass = value
        End Set
    End Property

    Public Property WebMethod() As String
        Get
            Return m_ctlWebMethod.Value
        End Get
        Set(ByVal value As String)
            m_ctlWebMethod.Value = value
        End Set
    End Property

    Public Property OnAutoCompleteSet() As String
        Get
            Return m_ctlOnAutoCompleteSet.Value
        End Get
        Set(ByVal value As String)
            m_ctlOnAutoCompleteSet.Value = value
        End Set
    End Property

    Public Sub New()
        MyBase.New()

        With m_ctlInputText
            .ID = Common.GetUniqueID(Context)
            .AutoCompleteType = WebControls.AutoCompleteType.Disabled
        End With

        With m_ctlInputValue
            .ID = Common.GetUniqueID(Context)
            .Style("display") = "none"
        End With

        With m_ctlOptionList
            .ID = Common.GetUniqueID(Context)
            .Style("display") = "none"
            .Style("position") = "fixed"
        End With

        With m_ctlSID
            .Name = "_SID"
            .Type = Variable.DataType.String
        End With

        With m_ctlParameter
            .Name = "_parameter"
            .Type = Variable.DataType.String
        End With

        With m_ctlWebMethod
            .Name = "_webMethod"
        End With

        With m_ctlOnAutoCompleteSet
            .Name = "_onAutoCompleteSet"
        End With

        Constructor = "new jObjective.AutoComplete($('#" & m_ctlInputText.ID & "'),$('#" & m_ctlInputValue.ID & "'),$('#" & m_ctlOptionList.ID & "'))"
    End Sub

    Protected Overrides Sub CreateChildControls()
        MyBase.CreateChildControls()

        With Controls
            .Add(m_ctlInputText)
            .Add(m_ctlInputValue)
            .Add(m_ctlOptionList)
            .Add(m_ctlSID)
            .Add(m_ctlParameter)
            .Add(m_ctlWebMethod)
            .Add(m_ctlOnAutoCompleteSet)
        End With

        ChildControlsCreated = True
    End Sub

    Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter)
        For Each objControl As Control In Me.Controls
            objControl.RenderControl(writer)
        Next
    End Sub

    Private Sub AutoComplete_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        Attributes("onkeyup") = String.Format("{0}.checkUpdate();", [Namespace])
        Attributes("onkeydown") = String.Format("{0}.handleKey((event.keyCode||event.which));", [Namespace])
        Attributes("onblur") = String.Format("{0}.hideSelectList();", [Namespace])

        With m_ctlOptionList
            .Attributes("onclick") = String.Format("{0}.setSelection();", [Namespace])
            .Attributes("onmouseover") = String.Format("{0}.removeInputBlur();", [Namespace])
            .Attributes("onmouseout") = String.Format("{0}.addInputBlur();", [Namespace])
        End With
    End Sub

    Public Class DataListRequest
        Inherits Request

        Public Input As String
        Public Parameter As String
    End Class

    Public Class DataItem
        Public Text As String
        Public Value As String
    End Class

    Public Class DataListResult
        Inherits Result

        Public DataList() As DataItem
    End Class
End Class